package com.gaff.emp.core.modules.perm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gaff.emp.core.EmpException;
import com.gaff.emp.core.modules.perm.controller.dept.form.CreateForm;
import com.gaff.emp.core.modules.perm.controller.dept.form.UpdateForm;
import com.gaff.emp.core.modules.perm.dao.SysDeptDao;
import com.gaff.emp.core.modules.perm.dao.SysUserDao;
import com.gaff.emp.core.modules.perm.entity.SysDept;
import com.gaff.emp.core.modules.perm.entity.SysUser;
import com.gaff.emp.core.modules.perm.service.SysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class SysDeptServiceImpl implements SysDeptService {

    @Autowired
    private SysDeptDao sysDeptDao;

    @Autowired
    private SysUserDao sysUserDao;

    @Override
    public List<SysDept> queryAllDepts() {
        List<SysDept> depts = sysDeptDao.selectList(Wrappers.emptyWrapper());
        List<SysDept> sysDepts = depts.stream()
                .filter(sysDept -> ObjectUtil.isNull(sysDept.getParentId()) || sysDept.getParentId() == 0)
                .collect(Collectors.toList());
        findSubDepts(sysDepts, depts);
        sysDepts.sort(Comparator.comparingInt(SysDept::getSort));
        return sysDepts;
    }

    @Override
    public SysDept queryDeptById(Integer id) {
        return sysDeptDao.selectById(id);
    }

    @Override
    public void createDept(CreateForm form) {
        SysDept dept = new SysDept();
        BeanUtil.copyProperties(form, dept);
        sysDeptDao.insert(dept);
    }

    @Override
    public void updateDept(UpdateForm form) {
        SysDept dept = sysDeptDao.selectById(form.getId());
        if (dept == null) {
            throw new EmpException("部门不存在");
        }
        SysDept parentDept = sysDeptDao.selectById(form.getParentId());
        if (parentDept != null && dept.getId() == parentDept.getId()) {
            throw new EmpException("上级部门不能是自身");
        }
        if (parentDept != null && dept.getParentId() == parentDept.getId() && parentDept.getId() == dept.getId()) {
            throw new EmpException("不能互为上级部门");
        }
        BeanUtil.copyProperties(form, dept);
        sysDeptDao.updateById(dept);
        sysUserDao.updateDeptNameByDeptId(dept.getId(), dept.getName());
    }

    @Override
    public void deleteById(Integer id) {
        SysDept dept = sysDeptDao.selectById(id);
        if (dept == null) {
            return;
        }
        List<SysDept> depts = sysDeptDao.selectList(Wrappers.<SysDept>query().eq("parent_id", id));
        if (depts != null && depts.size() > 0) {
            throw new EmpException("存在子部门，不能删除");
        }
        List<SysUser> users = sysUserDao.selectList(Wrappers.<SysUser>query().eq("dept_id", id));
        if (users != null && users.size() > 0) {
            throw new EmpException("部门被角色引用，不能删除");
        }
        sysDeptDao.deleteById(id);
    }

    private void findSubDepts(List<SysDept> sysDepts, List<SysDept> depts) {
        for (SysDept sysDept : sysDepts) {
            List<SysDept> subDepts = new ArrayList<>();
            for (SysDept dept : depts) {
                if (ObjectUtil.equal(sysDept.getId(), dept.getParentId())) {
                    dept.setParentName(sysDept.getName());
                    subDepts.add(dept);
                }
            }
            subDepts.sort(Comparator.comparingInt(SysDept::getSort));
            sysDept.setSubDepts(subDepts);
            findSubDepts(subDepts, depts);
        }
    }

}
